package com.simpleframe.domain.type;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.simpleframe.util.Utils;

public class TypeHtml {
	
	public String rodio(String name, String value, List<TypeItem> items){
		StringBuffer html = new StringBuffer();
		for (TypeItem typeItem : items) {
			html.append("<li>");
			html.append("<input type=\"radio\" alt=\"platform\" name=\""+name+"\" value=\""+typeItem.getValue()+"\" "+checked(typeItem, value)+" />");
			html.append(typeItem.getName());
			html.append("</li>");
		}
		return html.toString();
	}
	
	public String checkbox(String name, Long[] values, List<TypeItem> items){
		StringBuffer html = new StringBuffer();
		for (TypeItem typeItem : items) {
			html.append("<li>");
			html.append("<input type=\"checkbox\" name=\""+name+"\" value=\""+typeItem.getValue()+"\" "+checked(typeItem, values)+" />");
			html.append(typeItem.getName());
			html.append("</li>");
		}
		return html.toString();
	}
	
	public String select(String name, String value, List<TypeItem> items){
		StringBuffer html = new StringBuffer();
		html.append("<select name=\""+name+"\">");
		for (TypeItem typeItem : items) {
			html.append("<option value=\""+typeItem.getValue()+"\" "+selected(typeItem, value)+">"+typeItem.getName()+"</option>");
			html.append(typeItem.getName());
		}
		html.append("<select/>");
		return html.toString();
	}
	
	private String checked(TypeItem typeItem, Long[] values){
		String checked = "";
		if(Utils.isNull(values)){
			//checked = "checked=\"checked\"";
			return checked;
		}
		for (Long value : values) {
			if(StringUtils.equals(typeItem.getValue(), String.valueOf(value))){
				checked = "checked=\"checked\"";
				break;
			}
		}
		return checked;
	}
	
	private String checked(TypeItem typeItem, String value){
		String checked = "";
		if(Utils.isNull(value)){
			return checked;
		}
		if(StringUtils.equals(typeItem.getValue(), value)){
			checked = "checked=\"checked\"";
		}
		return checked;
	}
	
	private String selected(TypeItem typeItem, String value){
		String selected = "";
		if(Utils.isNull(value)){
			return selected;
		}
		if(StringUtils.equals(typeItem.getValue(), value)){
			selected = "selected=\"selected\"";
		}
		return selected;
	}
}
